package com.imooc.dynamic;

import com.imooc.pattern.TargetServer;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
@Slf4j
public class JDKDynamicProxy implements InvocationHandler{

    private TargetServer targetServer;

    public JDKDynamicProxy(TargetServer targetServer){
        this.targetServer = targetServer;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result = null;
        try{
            result = method.invoke(targetServer,args);
        }catch (Exception e){
            log.error("proxy exception",e.getMessage());
        }finally {
            log.info("Proxy after");
        }
        return result;
    }
}
